import {
	getBillStatus
} from "../api/chatDpt";
import { reBackIndex } from "./util";
import {
	WxUtil
} from "./wxutil";

export async function beginSearchPayStatus(clearUuid=true) {
	global.searchPayCount = 0;
	searchPayStatus(clearUuid)
}
const maxCount = 2;

async function searchPayStatus(clearUuid = true) {
	if (!uni.getStorageSync('UUID')) {
		return;
	}
	let UUID = uni.getStorageSync('UUID')
	global.searchPayCount += 1;
	const res = await getBillStatus(UUID);
	if (global.searchPayCount > 30) {
		uni.removeStorageSync('UUID')
		return;
	} else if (res === 'pending') {
		if(global.statusTime){
			clearTimeout(global.statusTime);
			global.statusTime = ''
		}
		global.statusTime =  setTimeout(() => {
			searchPayStatus(UUID)
		}, 3000)
	} else if (res) {
		clearUuid && uni.removeStorageSync('UUID')
		uni.showToast({
			title: "支付成功！", //显示的文字
			duration: 2000, //显示多少毫秒，默认1500毫秒，时间到自动隐藏
			icon: "success", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
		});
		if (res === 'menuPay') {
			setTimeout(() => {
				reBackIndex()
			}, 800)
			return;
		}
		setTimeout(() => {
			uni.redirectTo({
				url: '/pages/pay/orderResult?key=' + res
			})
		}, 800)
		return;
	}
}

export function stopSearchPay() {
	uni.removeStorageSync('UUID')
}

export function checkCanTy() {
	if(uni.getStorageSync('tyCount')<0){
		uni.showToast({
			title: "您没有试用机会了！", //显示的文字
			duration: 2000, //显示多少毫秒，默认1500毫秒，时间到自动隐藏
			icon: "error", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
		});
		return false;
	}
	if (uni.getStorageSync('tyCount') && uni.getStorageSync('tyCount') >= maxCount) {
		uni.showToast({
			title: "您没有试用机会了！", //显示的文字
			duration: 2000, //显示多少毫秒，默认1500毫秒，时间到自动隐藏
			icon: "error", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
		});
		return false;
	}
	return true;
}

export function addTyCount() {
	if (uni.getStorageSync('tyCount')) {
		uni.setStorageSync('tyCount', uni.getStorageSync('tyCount') + 1)
	} else {
		uni.setStorageSync('tyCount', 1)
	}
}

export function getTyCount() {
	if (uni.getStorageSync('tyCount')) {
		if (uni.getStorageSync('tyCount') > maxCount) {
			return 0;
		} else {
			return maxCount - uni.getStorageSync('tyCount')
		}
	}
	if(uni.getStorageSync('tyCount')<0){
		return 0;
	}
	return maxCount;
}

export function IsPC() {
	var userAgentInfo = navigator.userAgent;
	var Agents = ["Android", "iPhone",
		"SymbianOS", "Windows Phone",
		"iPad", "iPod"
	];
	var flagPc = true;
	for (var v = 0; v < Agents.length; v++) {
		if (userAgentInfo.indexOf(Agents[v]) > 0) {
			flagPc = false;
			break;
		}
	}
	return flagPc;
}
